package apis

import (
	"fmt"
	"google.golang.org/protobuf/proto"
	"mmo_game_zinx/core"
	"mmo_game_zinx/pb"
	"zinx/ziface"
	"zinx/znet"
)

type MoveApi struct {
	znet.BaseRouter
}

func (m *MoveApi) Handle(request ziface.IRequest) {
	// 解析客户端传传过来的proto协议
	reqMsg := &pb.Position{}
	err := proto.Unmarshal(request.GetData(), reqMsg)
	if err != nil {
		fmt.Println("move api data unmarshal, err:", err)
		return
	}
	// 得到当前发送位置的是哪个玩家
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("GetProperty pid error, ", err)
		return
	}
	fmt.Printf("Player pid= %d, move (%f, %f, %f, %f)\n", pid, reqMsg.X, reqMsg.Y, reqMsg.Z, reqMsg.V)
	currentPlayer := core.WorldMgrObj.GetPlayerByPid(pid.(int32))
	// 给其他玩家进行当前玩家的位置信息广播并更新当前玩家的位置
	currentPlayer.UpdatePosition(reqMsg.X, reqMsg.Y, reqMsg.Z, reqMsg.V)
}
